/**
 * @(#) reqSummaryMediator.as
 *
 * @date		2009. 04. 20.
 * @version		1.0
 * @author		Jo, ChangHun
 * @description	연말정산 메인 view - Mediator
 * 				연말정산 - 탭1n 개요
 */
package com.skt.common_hr.yearadjustment.view
{
	import com.skt.common_hr.common.PsHrAlert;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.yearadjustment.YearAdjustmentFacade;
	import com.skt.common_hr.yearadjustment.model.AddressProxy;
	import com.skt.common_hr.yearadjustment.model.PostProxy;
	import com.skt.common_hr.yearadjustment.model.vo.AddressVO;
	import com.skt.common_hr.yearadjustment.model.vo.PostSendVO;
	import com.skt.common_hr.yearadjustment.view.components.popPostNo;
	import com.skt.common_hr.yearadjustment.view.components.reqSummary;
	import com.wcl.utils.StringUtil;
	
	import flash.display.Sprite;
	import flash.events.Event;
	
	import mx.controls.Alert;
	import mx.core.IFlexDisplayObject;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	

	public class reqSummaryMediator extends Mediator
	{
		public static const NAME:String = "reqSummaryMediator"; 



		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function reqSummaryMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
            view.callLater(initView);
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		public function initView():void
 		{
 			view.addEventListener(reqSummary.CLICK_SEARCH_POSTNO, onPopUpPostNo);
 			view.addEventListener(reqSummary.CLICK_SAVE_ADDRESS, onClickSaveAddress);
 		}
 		
 		private var winPost:popPostNo = null;
 		private function onPopUpPostNo(event:Event):void{
 			var center:Sprite = Sprite(mx.core.Application.application);
 			
 			winPost = PopUpManager.createPopUp(center, popPostNo, true) as popPostNo;
 			PopUpManager.centerPopUp(winPost);
 			
 			
 			setPostPopUpEvents(winPost);
 			
 		}
 		
 		
 		private function setPostPopUpEvents(view:IFlexDisplayObject):void
 		{
 			view.addEventListener(popPostNo.CLICK_POST_SEARCH, onClickPostSearch);
 			view.addEventListener(popPostNo.CONFIRM_POPUP, onConfirmPostPopUp);
 			view.addEventListener(popPostNo.CANCEL_POPUP, onCancelPostPopUp);
 			view.addEventListener(popPostNo.LOAD_ADD_COMBOBOX, onLoadAddCombobox);
 		}
 		
 		/* 
 		private function onClickPostSearch(e:Event):void
 		{
 			var proxy:PostProxy = facade.retrieveProxy(PostProxy.NAME) as PostProxy;
 			proxy.load(winPost.txtSearchAddr.text);
 		}
 		*/
 		
 		private function onClickPostSearch(e:Event):void
 		{
 			var proxy:PostProxy = facade.retrieveProxy(PostProxy.NAME) as PostProxy;
 			
 			var param:Object={};
 			
 			param.I_GUBUN = PostSendVO.I_GUBUN;
 			param.I_STATE = PostSendVO.I_STATE;
 			param.I_SGGNM = PostSendVO.I_SGGNM;
 			param.I_TEXT = PostSendVO.I_TEXT;
 			param.I_TEXT2 = PostSendVO.I_TEXT2;
 			
 			proxy.load(param);
 		}

 		private function onLoadAddCombobox(e:Event):void{
 			
 			var proxy:PostProxy = facade.retrieveProxy(PostProxy.NAME) as PostProxy;
 			
 			var param:Object={};
 			
 			
 			if(winPost.addCombo1.dataProvider == null || winPost.addCombo1.dataProvider == ""){
	 			param.I_STATE = "";
 			}else{
	 			param.I_STATE = winPost.addCombo1.selectedItem.BLAND;
 			}
 			proxy.loadAddCombobox(param);
 		}
 		/* 
 		private function onConfirmPostPopUp(e:Event):void
 		{
 			var address:AddressVO = new AddressVO();
 			if(winPost.selected_post ==null) {
 				Alert.show('동이름을 입력해주세요.');
 				return;
 			}
 			address.LAND1 = view.editingAddress.LAND1;
 			address.LANDX50 = view.editingAddress.LANDX50;
 			address.HSHLD = view.editingAddress.HSHLD;
 			
 			address.STATE = winPost.selected_post.STATE;
 			address.BEZEI = winPost.selected_post.SIDO;
 			address.PSTLZ = winPost.selected_post.PSTLZ;	// 우편번호
 			address.ORT1K = winPost.selected_post.GU;
 			address.ORT2K = winPost.selected_post.DONG;
 			address.STRAS = winPost.selected_post.STRAS;
 			
 			view.editingAddress = address;
 			//view.txtLOCAT.setFocus();
 			
 			closePostPopUp();
 		}
 */
 
 		private function onConfirmPostPopUp(e:Event):void
 		{
 			var address:AddressVO = new AddressVO();
 			if(winPost.selected_post ==null) {
 				Alert.show('검색된 주소를 선택 해 주세요.');
 				
 				return;
 			}
 			address.LAND1 = view.editingAddress.LAND1;
 			address.LANDX50 = view.editingAddress.LANDX50;
 			address.HSHLD = view.editingAddress.HSHLD;
 			
 			address.STATE = winPost.selected_post.STATE;
 			address.BEZEI = winPost.selected_post.SIDO;
 			address.PSTLZ = winPost.selected_post.PSTLZ;	// 우편번호
 			address.ORT1K = winPost.selected_post.GU;
 			address.ORT2K = winPost.selected_post.DONG;
 			address.STRAS = winPost.selected_post.STRAS;
 			
 			view.editingAddress = address;
 			view.txtLOCAT.setFocus();
 			
 			closePostPopUp();
 		}

 		private function onCancelPostPopUp(e:Event):void
 		{
			closePostPopUp();
 		}
		
		private function closePostPopUp():void
		{
			PopUpManager.removePopUp(winPost);
		}
		
		private function loadAddress():void
		{
			var proxy:AddressProxy = facade.retrieveProxy(AddressProxy.NAME) as AddressProxy;
			proxy.load(Variables.yearPernr, view._selYear);
		}
		
		private function onClickSaveAddress(e:Event):void
		{
			if(validateAddress()) {
				var proxy:AddressProxy = facade.retrieveProxy(AddressProxy.NAME) as AddressProxy;
				var table:Array = new Array;
				table.push(view.editingAddress);
				proxy.save(Variables.yearPernr, view._selYear, table);
			} 
		}

		private function validateAddress():Boolean
		{			
			if(StringUtil.NVL(view.editingAddress.LOCAT)=="" && StringUtil.NVL(view.editingAddress.STRAS )=="") {
				PsHrAlert.showMessage("상세주소를 입력해 주십시오");
				return false;
			}			  
			return true; 
		}	
		
		/* 		
		private function validateAddress():Boolean
		{
			if(StringUtil.NVL(view.editingAddress.LOCAT)=="" && StringUtil.NVL(view.editingAddress.STRAS )=="") {
				PsHrAlert.showMessage("상세주소를 입력해 주십시오");
				return false;
			}			  
			return true; 
		}
		 */
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					PostProxy.LOAD_ADD_COMBOBOX_COMPLETE,
					PostProxy.LOAD_ADD_COMBOBOX2_COMPLETE,			
					PostProxy.LOAD_POST_COMPLETE,
					PostProxy.LOAD_POST_FAILED,
					AddressProxy.SAVE_ADDRESS_COMPLETE,
					AddressProxy.SAVE_ADDRESS_FAILED
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case PostProxy.LOAD_ADD_COMBOBOX_COMPLETE:
					winPost.addCombo1.dataProvider = (note.getBody() as PostProxy).addComboboxData;
					winPost.addCombo2.selectedIndex = -1;
					break;
				case PostProxy.LOAD_ADD_COMBOBOX2_COMPLETE:
					winPost.addCombo2.dataProvider = (note.getBody() as PostProxy).addComboboxData;
					winPost.addCombo2.enabled = true;
					break;				
				case PostProxy.LOAD_POST_COMPLETE:
					winPost.dataProvider = (note.getBody() as PostProxy).postList;
					break;
				case AddressProxy.SAVE_ADDRESS_COMPLETE:
					PsHrAlert.showMessage("주소가 저장되었습니다.");
					facade.sendNotification(YearAdjustmentFacade.RELOAD_BASIC_DATA);
					//loadAddress();
					view.vsAddress.selectedIndex = 0;
					break;
				case AddressProxy.SAVE_ADDRESS_FAILED:
					PsHrAlert.showMessage((note.getBody() as AddressProxy).msgStr);
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
 		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():reqSummary
        {
            return viewComponent as reqSummary;
        }
	}
}